package org.yfl.mbg.enhance.utils;

import org.apache.commons.lang3.SystemUtils;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.yfl.mbg.enhance.constants.CommonConstants;

import java.io.File;
import java.io.FilenameFilter;
import java.nio.file.Files;

/**
 * @author yanfeilin
 * @version 1.0.0
 * @date 2020/5/20 22:55
 */
public class FileUtil {
    private FileUtil(){

    }


    /**
     * 从classpath中获取数据库驱动文件路径
     * @param dbJarFileName
     * @return
     */
    public static String getDbJarFilePath(String dbJarFileName){
        String javaClassPath = SystemUtils.JAVA_CLASS_PATH;
        String dbJarFilePath = "";
        String[] classPathArr = null;
        if(SystemUtils.OS_NAME.contains("Windows")) {
            classPathArr = javaClassPath.split(CommonConstants.WINDOWS_CLASSPATH_SPLIT);
        }else{
            classPathArr = javaClassPath.split(CommonConstants.LINUX_CLASSPATH_SPLIT);
        }
        for (String classPath : classPathArr) {
            if(classPath.contains(dbJarFileName)){
                dbJarFilePath = classPath;
                break;
            }
        }
        return dbJarFilePath;
    }

    /**
     * 获取跟user.dir在同一个目录的工程路径
     * @param targetProjectName
     * @return
     */
    public static String getSameLevelProjectByUserDir(String targetProjectName){
        String userDir = SystemUtils.USER_DIR;
        if (userDir.contains(targetProjectName)){//当前工程路径
            return userDir;
        }
        File curDir = new File(userDir);
        File parentFile = curDir.getParentFile();
        String[] targetFilePathArr = parentFile.list((dir,name)->{
            if(name.equals(targetProjectName)){
                return true;
            }
            return false;
        });
        return targetFilePathArr[0];
    }

    /**
     * 文件是否存在
     * @param filePath
     * @return
     */
    public static boolean existsFile(String filePath){
        File file = new File(filePath);
        return file.exists();
    }

    /**
     * domain是否存在
     * @param context
     * @param introspectedTable
     * @return
     */
    public static boolean domainFileExists(Context context, IntrospectedTable introspectedTable){
        JavaModelGeneratorConfiguration modelGeneratorConfiguration = context
                .getJavaModelGeneratorConfiguration();
        String domainProjectPath = modelGeneratorConfiguration.getTargetProject();
        String domainPkg = modelGeneratorConfiguration.getTargetPackage();
        domainPkg = ClassUtil.pkg2path(domainPkg);
        String domainClsName = introspectedTable.getTableConfiguration().getDomainObjectName();

        StringBuilder domainJavaFilePath = new StringBuilder(128);
        domainJavaFilePath.append(domainProjectPath).append(domainPkg).append(File.separator)
                .append(domainClsName)
                .append(CommonConstants.JAVA_FILE_SUFFIX);
        if(existsFile(domainJavaFilePath.toString())){
            return true;
        }
        return false;
    }

    /**
     * 判断dao已经存在
     * @param context
     * @param introspectedTable
     * @return
     */
    public static boolean daoFileExists(Context context,IntrospectedTable introspectedTable){
        JavaClientGeneratorConfiguration clientGeneratorConfiguration = context
                .getJavaClientGeneratorConfiguration();
        String daoProjectPath = clientGeneratorConfiguration.getTargetProject();
        String daoFullClsName = introspectedTable.getMyBatis3JavaMapperType();
        daoFullClsName = ClassUtil.pkg2path(daoFullClsName);

        StringBuilder daoJavaFilePathBuilder = new StringBuilder(128);
        daoJavaFilePathBuilder.append(daoProjectPath).append(daoFullClsName).append(CommonConstants.JAVA_FILE_SUFFIX);
        if(existsFile(daoJavaFilePathBuilder.toString())){
            return true;
        }
        return false;
    }

    /**
     * 判断mapper xml文件已经存在
     * @param context
     * @param introspectedTable
     * @return
     */
    public static boolean sqlMapXmlExists(Context context,IntrospectedTable introspectedTable){
        SqlMapGeneratorConfiguration xmlGeneratorConfig = context
                .getSqlMapGeneratorConfiguration();
        String xmlProjectPath = xmlGeneratorConfig.getTargetProject();
        String xmlPkg = xmlGeneratorConfig.getTargetPackage();
        xmlPkg = ClassUtil.pkg2path(xmlPkg);
        String xmlFileName = introspectedTable.getMyBatis3XmlMapperFileName();

        StringBuilder daoJavaFilePathBuilder = new StringBuilder(128);
        daoJavaFilePathBuilder.append(xmlProjectPath).append(xmlPkg).append(File.separator)
                .append(xmlFileName);
        if(existsFile(daoJavaFilePathBuilder.toString())){
            return true;
        }
        return false;
    }
}
